package com.mike.bdb.ui.provider.tree;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mike.bdb.manager.DBManager;
import com.mike.bdb.util.ImageManager;

public class MikeTreeLabelProvider implements ILabelProvider {

	private static final Logger LOG = LoggerFactory.getLogger(MikeTreeLabelProvider.class);
	private static final String SPLIT = DBManager.BERKELEY_DATABASE_SECONDARY_INDEX_SPLIT_CHAR;
	@Override
	public Image getImage(Object obj) {
		if(obj instanceof TreeNode) {
			TreeNode treeNode = (TreeNode) obj;
			if(null == treeNode.getParent()) {
				return ImageManager.getImage(ImageManager.ICON_FOLDER);
			} else {
				return ImageManager.getImage(ImageManager.ICON_DATABASE);
			}
		}
		return null;
	}

	@Override
	public String getText(Object obj) {
		if(obj instanceof TreeNode) {
			TreeNode treeNode = (TreeNode) obj;
			String databaseName = treeNode.getName();
			if(databaseName.contains(SPLIT) && databaseName.contains(".")) {
				String[] split = databaseName.split("\\.");
				return split[split.length - 1];
			} else {
				String[] dbName = databaseName.split("\\\\");
				return dbName[dbName.length - 1];
			}
		}
		return null;
	}

	@Override
	public void dispose() {
	}

	@Override
	public boolean isLabelProperty(Object arg0, String arg1) {
		return false;
	}
	
	@Override
	public void addListener(ILabelProviderListener arg0) {
	}

	@Override
	public void removeListener(ILabelProviderListener arg0) {
	}

}
